package com.tarena.lbs.user.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.UserService;
import com.tarena.lbs.user.service.UserService;
import com.tarena.lbs.user.service.UserTagsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 手机小程序用户处理
 */
@Slf4j
@Api(tags = "用户接口")
@RestController
public class UserController {

    private final JwtEncoder jwtEncoder;

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminParamService;

    @Autowired
    private UserTagsService userTagsService;


    public UserController(JwtEncoder jwtEncoder) {
        this.jwtEncoder = jwtEncoder;
    }

    @ApiOperation(value = "用户注册")
    //小程序新增注册
    @PostMapping("/basic/user/info/register")
    public Result<Void> register(@RequestBody UserParam param)
        throws BusinessException{
        userService.save(param);
        return Result.success();
    }
    //解析jwt 拿到id获取登录用户性情
    @ApiOperation(value = "用户详情")
    @GetMapping("/basic/user/detail")
    public Result<UserVO> detail(@RequestHeader("Authorization") String token)throws BusinessException{
        UserPrinciple userPrinciple= (UserPrinciple) jwtEncoder.getLoginFromToken(token, UserPrinciple.class);
        UserVO userVO = userService.detail(userPrinciple.getId());
        return new Result<>(userVO);
    }


    //用户提交绑定关联所选标签
    @ApiOperation(value = "用户标签关联") //用户列表
    @GetMapping("/basic/user/tag")
    public Result<Void> bindUserTags(UserTagsParam param)
            throws BusinessException{
        log.debug("用户标签关联参数:{}", param);
        userTagsService.userTag(param);
        return Result.success();
    }
    @ApiOperation(value = "客户端注册")
    //客户端注册
    @PostMapping("/basic/user/info/adminRegister")
    public Result<Void> adminReg(@RequestBody AdminParam  adminParam)
            throws BusinessException{
        adminParamService.save(adminParam);
        return Result.success();
    }
}
